/*
 * @Author: xuetao 3168581151@qq.com
 * @Date: 2025-01-04
 * @LastEditors: xuetao 3168581151@qq.com
 * @LastEditTime: 2025-03-15
 * @Description: 布局容器
 */
import 'package:flutter/material.dart';
// 页面
import 'individual/individual_screen.dart';
import 'contact/contact_list_screen.dart';
import '../chat/chat_list_screen.dart';

class MobileHomeScreen extends StatefulWidget {
  const MobileHomeScreen({super.key});

  @override
  State<StatefulWidget> createState() {
    return _HomeScreenState();
  }
}

// 私有
class _HomeScreenState extends State<MobileHomeScreen> {
  // 底部导航栏页面列表
  final List<Widget> _pages = [
    const ChatListScreen(), // 聊天列表页面
    const ContactsScreen(), // 通讯录页面
    const IndividualScreen(), // 个人资料页面
  ];

  // 底部导航栏索引
  int _currentIndex = 0;

  // 控制页面切换的PageController
  final PageController _pageController = PageController();

  // 底部导航栏点击事件
  void _onTap(int index) {
    setState(() {
      _currentIndex = index;
    });
    _pageController.animateToPage(index,
        duration: const Duration(milliseconds: 300), curve: Curves.ease);
  }

  // 构建底部导航栏项
  BottomNavigationBarItem _buildBottomNavItem(IconData icon, String label) {
    return BottomNavigationBarItem(
      icon: Icon(
        icon,
        fill: 0.5,
        weight: 600,
      ),
      label: label,
    );
  }

  @override
  Widget build(BuildContext context) {
    return Scaffold(
      body: PageView(
        controller: _pageController,
        onPageChanged: (index) {
          setState(() {
            _currentIndex = index;
          });
        },
        children: _pages,
      ),
      bottomNavigationBar: BottomNavigationBar(
        type: BottomNavigationBarType.fixed, // item>=4个时样式丢失
        currentIndex: _currentIndex,
        onTap: _onTap,
        items: [
          _buildBottomNavItem(Icons.mode_comment, '消息'),
          _buildBottomNavItem(Icons.contacts, '通讯录'),
          _buildBottomNavItem(Icons.manage_accounts, '我'),
        ],
      ),
    );
  }
}
